package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.Project;
import cn.bx.bid.service.ProjectService;
import cn.bx.bid.util.LogUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
public class ProjectController {

    //@Value("f:/upload")
    @Value("${uploadPath}")//SPel:

    private String uploadPath;

    @Resource
    private ProjectService projectService;

    @GetMapping("toProjectAdd")
    public String toAdd(){
        return "projectAdd";
    }

    @PostMapping("projectAdd")
    public String add(HttpSession s, @ModelAttribute("p") Project p, @RequestParam(name = "fujian",required = false) MultipartFile file){
        LogUtil.info("fffffffffff="+file+",保存在:"+uploadPath);
        if(!(file==null||file.isEmpty())){
            //文件后缀 getName()
            String oldName=file.getOriginalFilename();
            String prefix=oldName.substring(oldName.lastIndexOf('.'));
            //新文件名
            String newName= UUID.randomUUID()+prefix;
            //保存到项目的doc目录下
            //springboot,没有.getRealpath是运行目录.
            //指定到固定的目录
            //File path=new File(s.getServletContext().getRealPath("doc"));
            File path=new File(uploadPath);
            LogUtil.info("文件的路径："+path.getAbsolutePath());
            if(path.exists()==false)
                path.mkdirs();

            try {
                file.transferTo(new File(path,newName));
                p.setProjectFilePath(newName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        LogUtil.info("新项目的信息时：\n"+p);
        return "addSuccess";
    }

    //掉渣
//    @GetMapping("/projectSearch")
//    public Page<Project> paging(Writer resp, @RequestParam(name="page",defaultValue = "1") int pageNo, @RequestParam(name="limit",defaultValue = "10") int limit, @RequestParam(name="name",required = false)String name, @RequestParam(name="category",required = false) String category) {
//
//
//        Page<Project> p =this.projectService.paging(pageNo,limit,name,category);
//        String s=JSON.toJSONString(p);
//        resp.write(s);
//        return p;
//
//
//    }
    @GetMapping(value="/projectSearch",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<Project> paging(@RequestParam(name="page",defaultValue = "1") int pageNo, @RequestParam(name="limit",defaultValue = "10") int limit, @RequestParam(name="name",required = false)String name, @RequestParam(name="category",required = false) String category) {
        LogUtil.info("ffffffffff保存在:"+uploadPath);
        Page<Project> p =this.projectService.paging(pageNo,limit,name,category);
        return p;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,"registerDate", new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值

        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH");
        binder.registerCustomEditor(Date.class,"openDate", new CustomDateEditor(dateFormat2, true));

    }
}
